package wzh.crm.ui.menu;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import wzh.crm.ui.contact.ContactListActivity;
import wzh.crm.ui.contract.ContractListActivity;
import wzh.crm.ui.customer.CustomerListActivity;
import wzh.crm.R;
import wzh.crm.ui.opportunity.OpportunityListActivity;
import wzh.crm.ui.product.ProductListActivity;

public class MenuActivity extends AppCompatActivity {
    List<MenuItem> menuItem=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        buildMenuItemList();
        setMenuItemText();
        setMenuItemListener();
    }

    private void buildMenuItemList(){
        menuItem.add((MenuItem) findViewById(R.id.activity_menu_menuitem1));
        menuItem.add((MenuItem) findViewById(R.id.activity_menu_menuitem2));
        menuItem.add((MenuItem) findViewById(R.id.activity_menu_menuitem3));
        menuItem.add((MenuItem) findViewById(R.id.activity_menu_menuitem4));
        menuItem.add((MenuItem) findViewById(R.id.activity_menu_menuitem5));
        menuItem.add((MenuItem) findViewById(R.id.activity_menu_menuitem6));
    }

    private void setMenuItemText(){
        String[][] data= buildMenuItemText();
        for(int i=0;i<menuItem.size();i++){
            MenuItem thisItem=menuItem.get(i);
            try {
                String[] thisData = data[i];
                thisItem.setTitle(thisData[0]);
                thisItem.setDetail(thisData[1]);
            }catch (IndexOutOfBoundsException e){
                thisItem.setVisibility(View.INVISIBLE);
            }
        }
    }

    private void setMenuItemListener(){
        View.OnClickListener[] listeners=getMenuItemListener();
        for(int i=0;i<menuItem.size();i++){
            MenuItem thisItem=menuItem.get(i);
            try {
                if(listeners[i]==null){
                    continue;
                }
                thisItem.setOnClickListener(listeners[i]);
            }catch (IndexOutOfBoundsException e){
                continue;
            }
        }
    }

    protected String[][] buildMenuItemText(){
        return new String[][]{
                {"公司客户",""},
                {"公司商机",""},
                {"已创建合同",""},
                {"业务管理",""},
                {"联系人",""},
                {"公司产品",""}
        };
    }

    protected View.OnClickListener[] getMenuItemListener(){
        return new View.OnClickListener[]{
                buildStartActivityOnClickListener(CustomerListActivity.class),
                buildStartActivityOnClickListener(OpportunityListActivity.class),
                buildStartActivityOnClickListener(ContractListActivity.class),
                buildStartActivityOnClickListener(null),
                buildStartActivityOnClickListener(ContactListActivity.class),
                buildStartActivityOnClickListener(ProductListActivity.class)
        };
    }

    protected View.OnClickListener buildStartActivityOnClickListener(final Class<?> activity){
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(MenuActivity.this, activity);
                MenuActivity.this.startActivity(i);
            }
        };
    }
}
